//
//  SetViewController.m
//  fruitsbuy3
//
//  Created by sgone-2 on 15/8/21.
//  Copyright (c) 2015年 sgone. All rights reserved.
//

#import "SetViewController.h"
#import "Common.h"
#import "DataSingleton.h"
//#import "BPush.h"
//#import "PersonalCenterViewController.h"

#import "OurViewController.h"
#import "IntroductionController.h"

#import "UMFeedback.h"

#import "LoginViewController.h"
#import "DoorMessage.h"
#import "ZHAQController.h"
#import "XGMMViewController.h"
@interface SetViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong) DataSingleton *dataHandler;

@property(nonatomic,strong) UIView *topView;
@property(nonatomic,strong) UILabel *xinxiLab;
@property(nonatomic,strong) UIButton *backBut;
@property(nonatomic,strong) UIImageView *titleImage;
@property(nonatomic,strong) UIButton*logoutBut;
@end

@implementation SetViewController





//隐藏tabbar
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}



-(void)viewWillAppear:(BOOL)animated
{
    

}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置状态栏的字体颜色为白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    self.dataHandler = [DataSingleton sharedDataSingleton];
    
    
    self.tableView.scrollEnabled = NO;
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, 64)];
    self.topView.backgroundColor = BenSe_COLOR;
    [self.view addSubview:self.topView];
    self.xinxiLab = [[UILabel alloc]init];
    [self.xinxiLab sizeToFit];
    self.xinxiLab.frame = CGRectMake(0,40,320 ,30);
    self.xinxiLab.center = CGPointMake(self.topView.bounds.size.width/2, self.topView.bounds.size.height-24);
    
    self.xinxiLab.backgroundColor = [UIColor clearColor];
    self.xinxiLab.textAlignment =  NSTextAlignmentCenter;
    self.xinxiLab.textColor = [UIColor whiteColor];
//    self.xinxiLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:19];
    self.xinxiLab.font = [UIFont systemFontOfSize:19];
    self.xinxiLab.text = @"设置";
    [self.topView addSubview:self.xinxiLab];
    

    
    [self backMain1];

    

}

-(void)backMain1
{
    
    self.backBut=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.backBut addTarget:self action:@selector(back1:) forControlEvents:UIControlEventTouchUpInside];
    [self.backBut setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self.backBut setFrame:CGRectMake(0, 14, 50, 50)];
    [self.topView addSubview:self.backBut];
    
    
}
- (void)back1:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 2;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 0) {
        
        return 2;
    }
    else
    {
    
        return 3;
    
    }
    

}




-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        
        return 0.01;
    }
    else
    {
    
        return 10;
    }
 

}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        
        
        return 200;
    }
    else
    {
        return 0.01;
    
    }

}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{

    if (section == 1) {
        
        UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
        view2.backgroundColor  = BACKGROUND1;
        [self.view addSubview:view2];
        
        
        self.logoutBut = [[UIButton alloc]initWithFrame:CGRectMake((ViewWidth )*0.15, 80/FDLaOt, ViewWidth * 0.7 , 35 / FDLaOt)];
        [self.logoutBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.logoutBut.titleLabel.font = [UIFont boldSystemFontOfSize:17];
       
        
        self.logoutBut.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.logoutBut setTitle:@"退出登录" forState:UIControlStateNormal];
        [self.logoutBut addTarget:self action:@selector(tuiChu:) forControlEvents:UIControlEventTouchUpInside];
        self.logoutBut.backgroundColor = BenSe_COLOR;
        self.logoutBut.layer.masksToBounds = YES;
        self.logoutBut.layer.cornerRadius = 5.0;
        [view2 addSubview:self.logoutBut];
        
        return view2;
    }
    else
    {
    
        return nil;
    
    }
   
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{




    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    self.titleImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 12, 19, 19)];
    [cell addSubview:self.titleImage];
    
    UILabel *cellLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, 120, 44)];
    cellLabel.backgroundColor = [UIColor clearColor];
    cellLabel.font = [UIFont systemFontOfSize:15];
    
    [cell addSubview:cellLabel];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0) {
            
            cell.textLabel.text = @"功能介绍";
        }
        else if (indexPath.row == 1)
        {
          
          cell.textLabel.text = @"密码设置";
        }
    
    
    }

    if (indexPath.section == 1) {
        if(indexPath.row == 0)
        {
            
            
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//            self.titleImage.image = [UIImage imageNamed:@"set2"];
//            cellLabel.text = @"版本版号";
            
            UILabel *subLabel = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.75, 0, 150, 44)];
            
            //获取当前版本号
            NSDictionary *infoDic = [[NSBundle mainBundle]infoDictionary];
            NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
            NSString *number = [NSString stringWithFormat:@"%@ 版本",currentVersion];
            subLabel.textColor = [UIColor grayColor];
            subLabel.font = [UIFont systemFontOfSize:15];
            // subLabel.backgroundColor = [UIColor clearColor];
            subLabel.text = number;
            
            [cell addSubview:subLabel];
            cell.textLabel.text = @"当前版本";

            
          
            
        }
        else if (indexPath.row == 1)
        {
            
//            self.titleImage.image = [UIImage imageNamed:@"set1"];
//            cellLabel.text = @"用户反馈";
            cell.textLabel.text = @"用户反馈";

        }
        else
        {
//            self.titleImage.image = [UIImage imageNamed:@"set3"];
//            cellLabel.text = @"关于我们";
            cell.textLabel.text = @"关于我们";
    

        }
        
    }
    
    
  /*
    else if (indexPath.section == 2 )
    {
    
        if (indexPath.row == 0)
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor = BACKGROUND1;

  
        
      self.logoutBut = [[UIButton alloc]initWithFrame:CGRectMake((ViewWidth )*0.2, 0, ViewWidth * 0.6 , 30 / FDLaOt)];
        [self.logoutBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.logoutBut.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.logoutBut.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.logoutBut setTitle:@"退出登录" forState:UIControlStateNormal];
        [self.logoutBut addTarget:self action:@selector(tuiChu:) forControlEvents:UIControlEventTouchUpInside];
        self.logoutBut.backgroundColor = BenSe_COLOR;
        self.logoutBut.layer.masksToBounds = YES;
        self.logoutBut.layer.cornerRadius = 5.0;
        [cell addSubview:self.logoutBut];
        
    }
*/
    return cell;

}


#pragma mark 设置点击cell触摸事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
            IntroductionController *introduction = [[IntroductionController alloc]initWithNibName:nil bundle:nil];
           [self.navigationController pushViewController:introduction animated:YES];

    }
    else if (indexPath.row == 1)
    {
    
//        ZHAQController * zh = [[ZHAQController alloc]initWithNibName:nil bundle:nil];
//        [self.navigationController pushViewController:zh animated:YES];

        XGMMViewController *mm = [[XGMMViewController alloc]initWithNibName:nil bundle:nil];
        [self.navigationController pushViewController:mm animated:YES];
    
    }}
        
    if (indexPath.section == 1) {
        
        if (indexPath.row == 0)
        {
        
            return;
        }
            
        
    else if  (indexPath.row == 1)
    {
            [self.navigationController pushViewController:[UMFeedback feedbackViewController] animated:YES];
    }

    else  if (indexPath.row == 2)
        
    {
        OurViewController *our = [[OurViewController alloc]initWithNibName:nil bundle:nil];
        [self.navigationController pushViewController:our animated:YES];
    }
    }
    







}

-(void)tuiChu:(id)sender
{



    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"  message:@"您确定退出登录么？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    [alert show];
    
    alert.tag=2;



}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (alertView.tag== 2)  {
        if(buttonIndex==1)
        {
            
            

            [self.dataHandler removeLocalData:@"wTel"];
            [self.dataHandler removeLocalData:@"utoken"];
            [self.dataHandler removeLocalData:@"data"];
            
            
            
            //将所有存储的数据进行清除，并且跳转到登录界面
//            [self.dataHandler removeLocalData:@"wTel"];
//            [self.dataHandler removeLocalData:@"password"];
//            [self.dataHandler removeLocalData:@"uToken"];
//            [self.dataHandler removeLocalData:@"wholesaler_name"];
//            [self.dataHandler removeLocalData:@"address"];
//            [self.dataHandler removeLocalData:@"address1"];
//            [self.dataHandler removeLocalData:@"city"];
//            [self.dataHandler removeLocalData:@"market"];
//            [self.dataHandler removeLocalData:@"push_radius"];
//            [self.dataHandler removeLocalData:@"start_price"];
//            [self.dataHandler removeLocalData:@"near_range"];
//            [self.dataHandler removeLocalData:@"start_point"];
//            [self.dataHandler removeLocalData:@"over_price"];
//            [self.dataHandler removeLocalData:@"addX"];
//            [self.dataHandler removeLocalData:@"addY"];
//            [self.dataHandler removeLocalData:@"data"];

            
            
            

            self.logoutBut.hidden = YES;
            
            
            
            //t跳转到登录
            LoginViewController *login = [[LoginViewController alloc]initWithNibName:nil bundle:nil];
            
            [self presentViewController:login animated:YES completion:nil];
            
 
            
            

            
            
            
            
           
            
            //通知告知退出登录
          [[NSNotificationCenter defaultCenter] postNotificationName:@"tuichu" object:nil userInfo:nil];
            
   
            [self.navigationController popViewControllerAnimated:YES];
            
           
            
        }
        
    }
    
    
}


@end
